<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :lg="8" :md="8" :sm="12" :xl="8" :xs="12" />
      <vab-query-form-right-panel :lg="16" :md="16" :sm="12" :xl="16" :xs="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.condition.exceptionType"
              clearable
              placeholder="请输入异常类型"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" label="id" prop="id" width="100" />
      <el-table-column
        align="center"
        label="路径"
        prop="optUrl"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="方法" prop="methodName" />
      <el-table-column align="center" label="异常类型" prop="exceptionType" />
      <el-table-column align="center" label="报错时间" prop="createTime" />
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="100"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">详情</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="600px"
      @close="close"
    >
      <div class="exception-log-msg">
        <el-input :rows="10" type="textarea" :value="exceptionMsg" />
      </div>
      <template #footer>
        <el-button @click="close">关 闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { getExceptionLogs } from '@/api/sys'

  export default {
    name: 'ExceptionLog',
    components: {},
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          condition: {
            orderBy: 'id',
            orderDirect: 'desc',
            exceptionType: null,
          },
          pageNum: 1,
          pageSize: 10,
        },
        title: '异常详情',
        dialogFormVisible: false,
        exceptionMsg: '',
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      close() {
        this.exceptionMsg = ''
        this.dialogFormVisible = false
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        this.exceptionMsg = row.exceptionMsg
        this.dialogFormVisible = true
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const res = await getExceptionLogs(this.queryForm)
          if (res && res.data) {
            const { list, total } = res.data
            this.list = list
            this.total = total
          }
        } catch (e) {
          console.log(e)
        }
        this.listLoading = false
      },
    },
  }
</script>
<style lang="scss" scoped>
  .exception-log-msg {
    max-height: 500px;
  }
</style>
